package com.shelpe.services.algorithm.entity.jpa;

import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.apache.commons.lang.math.NumberUtils;

import com.shelpe.services.algorithm.utils.EntityUtils;

import lombok.Data;

@Entity(name = "tactics")
@Data
public class Tactics {
	@Id
	@Column(name="tactics_id")
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long tacticsID;
	@Column(name="user_id")
	private long userID;
	@Column(name="tactics_name")
	private String name;
	@Column(name="tactics_status")
	@Convert(converter=StatusConverter.class)
	private Status status;
	private int executions;
	@Convert(converter=PremiumConverter.class)
	private Premium premium;
	private int campaignPeriod;
	private int heighestPrice;
	@Column(name="keyword_ids")
	private String keywordIDs;
	@Column(name="tactics_create_time")
	@Convert(converter=EntityUtils.Day2IntConverter.class)
	private Date createTime;
	@Column(name="tactics_modified_time")
	@Convert(converter=EntityUtils.Day2IntConverter.class)
	private Date modifiedTime;
	private String path;
	@Column(name="`from`")
	@Convert(converter=EntityUtils.Day2StringConverter.class)
	private Date fromTime;
	@Column(name="`to`")
	@Convert(converter=EntityUtils.Day2StringConverter.class)
	private Date toTime;
	
	private Date nextTime;
	
	private int device;
	@Data
	public static class Premium{
		private float value;
		private boolean percent;
		
		public int nextPrice(int price){
			if(percent){
				price = (int) (price * (value + 100) / 100);
			}else{
				price = price + (int) value;
			}
			return price;
		}
	}
	
	public static enum Status{
		starting(1), pause(2), finish(3);
		private int value;

		private Status(int v) {
			this.value = v;
		}

		public int getValue() {
			return this.value;
		}
	}
	
	public static class PremiumConverter implements AttributeConverter<Premium, String> {

		@Override
		public String convertToDatabaseColumn(Premium attribute) {
			if(attribute != null){
				return attribute.getValue() + (attribute.percent?"%":"");
			}
			return null;
		}

		@Override
		public Premium convertToEntityAttribute(String dbData) {
			if(dbData != null){
				Premium p = new Premium();
				if(dbData.contains("%")){
					dbData = dbData.replace("%", "");
					p.percent = true;
				}
				p.value = NumberUtils.toFloat(dbData);
				return p;
			}
			return null;
		}
		
	}
	
	public static class StatusConverter implements AttributeConverter<Status, Integer> {

		@Override
		public Integer convertToDatabaseColumn(Status attribute) {
			return attribute != null ? attribute.getValue() : Status.starting.getValue();
		}

		@Override
		public Status convertToEntityAttribute(Integer dbData) {
			Status status = Status.starting;
			for (Status s : Status.values()) {
				if (s.getValue() == dbData) {
					status = s;
					break;
				}
			}
			return status;
		}
	}
	
}
